<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/23
 * Time: 09:05
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use think\Exception;

/**
 * 题库类别管理
 * Class Category
 * @package app\admin\controller
 */
class Category extends Backend
{
    protected $model = null;
    protected $category_list = [];
    public function _initialize(){
        parent::_initialize();
        $this->model = model('Category');
        $parent_category_list = $this->model->where(array('parent_id'=>0))->select();
        $category_list = [];
        $category_list[0] = '无';
        foreach ($parent_category_list as $key=>$value){
            $category_list[$value['id']] = $value['name'];
        }
        $this->view->assign('category_list',$category_list);
    }

    /**
     * 查看
     */
//    public function index()
//    {
//        if ($this->request->isAjax())
//        {
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//            $total = $this->model
//                ->where($where)
//                ->order($sort, $order)
//                ->count();
//
//            $list = $this->model
//                ->where($where)
//                ->order($sort, $order)
//                ->limit($offset, $limit)
//                ->select();
//            foreach ($list as $key=>&$value){
//                $value['parent_name'] = $this->model->where(array('id'=>$value['parent_id']))->value('name');
//            }
//            unset($value);
//            $result = array("total" => $total, "rows" => $list);
//
//            return json($result);
//        }
//        $category_list = collection($this->model->order('id', 'desc')->select())->toArray();
//        unset($v);
//        $tree =  Tree::instance()->init($category_list);
//        $tree->pidname = 'parent_id';
//        $this->category_list = $tree->getTreeList($tree->getTreeArray(0), 'name');
//        $node_data = [0 => __('None')];
//        foreach ($this->category_list as $k => &$v)
//        {
//            if ($v['parent_id'] != 0)
//                continue;
//            $node_data[$v['id']] = $v['name'];
//        }
//        $this->view->assign('category_list', $node_data);
//        return $this->view->fetch();
//    }

    public function index(){
        if ($this->request->isAjax())
        {
            $category_list = collection($this->model->order('id', 'desc')->select())->toArray();
            $tree =  Tree::instance()->init($category_list);
            $tree->pidname = 'parent_id';
            $list = $tree->getTreeList($tree->getTreeArray(0), 'name');
//            $node_data = [0 => __('None')];
            foreach ($list as $k => &$v)
            {
                $v['parent_name'] = $this->model->where(array('id'=>$v['parent_id']))->value('name');
            }
            unset($v);
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }

        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $this->model->create($params);
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $row->save($params);
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            Db::startTrans();
            try {
                $result = $this->model->destroy($ids);
                if($result){
                    Db::commit();
                    $this->success('删除成功!');
                }else {
                    $this->error('删除失败!');
                }
            }catch (Exception $e){
                Db::rollback();
                $this->error('删除失败!');
            }

        }
        $this->error();
    }
}